from django import forms
from web.models import Comment

class CommentForm(forms.ModelForm):
    def __init__(self, captcha, *args, **kwargs):
        self.captcha = captcha
        super(CommentForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Comment
        fields = ('autor', 'email', 'web', 'content', 'captcha')

    captcha = forms.CharField(
        help_text='<img src="/captcha/comment_captcha/" title="Captcha" alt="Captcha" />'
    )

    def clean_captcha(self):
        if self.cleaned_data.get('captcha') == self.captcha:
            return u''
        raise forms.ValidationError('Invalid captcha.')
